<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>git基本操作</title>
    <meta name="description" content="陈康康的博客">
    
    
    <link rel="preload" href="/ibooks/assets/css/0.styles.c1119b7f.css" as="style"><link rel="preload" href="/ibooks/assets/js/app.2295cc0e.js" as="script"><link rel="preload" href="/ibooks/assets/js/2.8363fd35.js" as="script"><link rel="preload" href="/ibooks/assets/js/9.105f4e04.js" as="script"><link rel="prefetch" href="/ibooks/assets/js/10.06160dd2.js"><link rel="prefetch" href="/ibooks/assets/js/3.a24e0991.js"><link rel="prefetch" href="/ibooks/assets/js/4.519afd95.js"><link rel="prefetch" href="/ibooks/assets/js/5.e3e0cc6b.js"><link rel="prefetch" href="/ibooks/assets/js/6.3538e568.js"><link rel="prefetch" href="/ibooks/assets/js/7.889ab10b.js"><link rel="prefetch" href="/ibooks/assets/js/8.20b2c716.js">
    <link rel="stylesheet" href="/ibooks/assets/css/0.styles.c1119b7f.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/ibooks/" class="home-link router-link-active"></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/ibooks/" class="nav-link">主页</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">技术笔记</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ibooks/frontend/framework/vue/" class="nav-link">前端开发笔记</a></li><li class="dropdown-item"><!----> <a href="/ibooks/otherstudy/git/base-operation.html" class="nav-link router-link-exact-active router-link-active">其他学习</a></li></ul></div></div><div class="nav-item"><a href="/ibooks/codepit/" class="nav-link">踩坑记录</a></div><div class="nav-item"><a href="/ibooks/about/" class="nav-link">关于</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/ibooks/" class="nav-link">主页</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">技术笔记</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ibooks/frontend/framework/vue/" class="nav-link">前端开发笔记</a></li><li class="dropdown-item"><!----> <a href="/ibooks/otherstudy/git/base-operation.html" class="nav-link router-link-exact-active router-link-active">其他学习</a></li></ul></div></div><div class="nav-item"><a href="/ibooks/codepit/" class="nav-link">踩坑记录</a></div><div class="nav-item"><a href="/ibooks/about/" class="nav-link">关于</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>其他学习</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>git</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ibooks/otherstudy/git/base-operation.html" class="active sidebar-link">git基本操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/ibooks/otherstudy/git/base-operation.html#git-init" class="sidebar-link">git init</a></li><li class="sidebar-sub-header"><a href="/ibooks/otherstudy/git/base-operation.html#git-add" class="sidebar-link">git add</a></li><li class="sidebar-sub-header"><a href="/ibooks/otherstudy/git/base-operation.html#git-commit" class="sidebar-link">git commit</a></li><li class="sidebar-sub-header"><a href="/ibooks/otherstudy/git/base-operation.html#git-remote" class="sidebar-link">git remote</a></li><li class="sidebar-sub-header"><a href="/ibooks/otherstudy/git/base-operation.html#git-push" class="sidebar-link">git push</a></li><li class="sidebar-sub-header"><a href="/ibooks/otherstudy/git/base-operation.html#git-checkout" class="sidebar-link">git checkout</a></li></ul></li></ul></section></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="git基本操作"><a href="#git基本操作" aria-hidden="true" class="header-anchor">#</a> git基本操作</h1> <p>示例：github新建一个项目自带的quick tip。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># create new repository</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;# test&quot;</span> <span class="token operator">&gt;&gt;</span> REAMME.md
<span class="token function">git</span> init
<span class="token function">git</span> <span class="token function">add</span> README.md
<span class="token function">git</span> commit -m <span class="token string">&quot;first commit&quot;</span>
<span class="token function">git</span> remote <span class="token function">add</span> origin xxxx
<span class="token function">git</span> push -u origin master

<span class="token comment"># push existing repository</span>
<span class="token function">git</span> remote <span class="token function">add</span> origin xxxx
<span class="token function">git</span> push -u origin master
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="git-init"><a href="#git-init" aria-hidden="true" class="header-anchor">#</a> git init</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 新建一个git代码库</span>
<span class="token comment"># 会在项目根目录创建一个.git目录</span>
<span class="token function">git</span> init
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="git-add"><a href="#git-add" aria-hidden="true" class="header-anchor">#</a> git add</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 添加指定文件到暂存区</span>
<span class="token function">git</span> <span class="token function">add</span> README.md

<span class="token comment"># 添加指定文件夹到暂存区</span>
<span class="token function">git</span> <span class="token function">add</span> <span class="token punctuation">[</span>dir<span class="token punctuation">]</span>

<span class="token comment"># 添加当前目录的所有文件到暂存区</span>
<span class="token function">git</span> <span class="token function">add</span> <span class="token builtin class-name">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="git-commit"><a href="#git-commit" aria-hidden="true" class="header-anchor">#</a> git commit</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># -m 添加提交说明</span>
<span class="token function">git</span> commit -m xxx

<span class="token comment"># --amend 追加提交</span>
<span class="token function">git</span> commit --amend

<span class="token comment"># -a 先将所有变动提交到暂存区，没有-m会打开编辑器 让用户提交说明</span>
<span class="token function">git</span> commit -am xxx

<span class="token comment"># --allow-empty 用于没有提交信息的commit，不建议没有提交信息</span>
<span class="token function">git</span> commit --allow-empty
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="git-remote"><a href="#git-remote" aria-hidden="true" class="header-anchor">#</a> git remote</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 添加远程git地址</span>
<span class="token function">git</span> remote <span class="token function">add</span> origin xxx

<span class="token comment"># 删除远程git地址（remove可用rm代替）</span>
<span class="token function">git</span> remote remove origin
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="git-push"><a href="#git-push" aria-hidden="true" class="header-anchor">#</a> git push</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 将本地代码推送到远程分支 -u 指定origin为默认主机 后面推送可以不带任何参数</span>
<span class="token function">git</span> push -u origin xxx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="git-checkout"><a href="#git-checkout" aria-hidden="true" class="header-anchor">#</a> git checkout</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 切换分支</span>
<span class="token function">git</span> checkout xxx

<span class="token comment"># 创建一个新分支 -b</span>
<span class="token function">git</span> checkout -b xxx

<span class="token comment"># 当前目录所有修改的文件 从head签出并恢复未修改时的样子</span>
<span class="token comment"># head是一个指针，指向当前分支</span>
<span class="token function">git</span> checkout <span class="token builtin class-name">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>对于已存在的git代码库，一般的开发流程：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 开发之前</span>
<span class="token comment"># 先clone源代码</span>
<span class="token function">git</span> clone xxx.git

<span class="token comment"># 创建自己的任务分支</span>
<span class="token function">git</span> checkout -b task-xxx

<span class="token comment"># 提交自己的代码</span>
<span class="token function">git</span> <span class="token function">add</span> <span class="token builtin class-name">.</span>
<span class="token function">git</span> commit -m <span class="token string">'finish feature xxx'</span>

<span class="token comment"># 可能多人协作，提交之前先拉下代码</span>
<span class="token function">git</span> pull origin task-xxx

<span class="token comment"># 如果拉下来的代码有冲突, 查看下冲突文件，手动解决下冲突</span>
<span class="token comment"># -uno 只列出已被git管理切被修改但没提交的文件</span>
<span class="token function">git</span> status -uno

<span class="token comment"># 最后推送自己的提交</span>
<span class="token function">git</span> push origin task-xxx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>还有一些场景，比如说正在开发一个功能，临时有一个需求要插进来，但是不想提交当前进行中的工作，可以使用stash暂存我们的修改</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">git</span> stash
<span class="token function">git</span> status
<span class="token function">git</span> checkout task-xxx-2
<span class="token function">git</span> pull
<span class="token function">git</span> checkout task-xxx-1
<span class="token function">git</span> stash pop
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="/ibooks/assets/js/app.2295cc0e.js" defer></script><script src="/ibooks/assets/js/2.8363fd35.js" defer></script><script src="/ibooks/assets/js/9.105f4e04.js" defer></script>
  </body>
</html>
